package com.kingdee.eas.custom.wg.reports.app;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.eas.basedata.master.material.MaterialInfo;
import com.kingdee.eas.basedata.org.StorageOrgUnitInfo;
import com.kingdee.eas.custom.wg.sellplan.billstatus;
import com.kingdee.eas.framework.report.util.RptParams;
import com.kingdee.eas.framework.report.util.RptRowSet;
import com.kingdee.util.DateTimeUtils;

public class ReportFacadeControllerBean extends AbstractReportFacadeControllerBean
{
    private static Logger logger =
        Logger.getLogger("com.kingdee.eas.custom.wg.reports.app.ReportFacadeControllerBean");
    protected RptParams _collectSalePan(Context ctx, RptParams params)throws BOSException
    {    
    	StringBuffer condition=new StringBuffer(" where 1=1 ");
       	Object[] storageOrgrequest =(Object[]) params.getObject("storageOrgrequest");
    	Object[] storageOrgprovide =(Object[]) params.getObject("storageOrgprovide");
       	String temp="and T_SEL_SellPlan.FStorageOrgRequest in (";
       	boolean flag=false;
       	for(int i=0 ;storageOrgrequest!=null&&storageOrgrequest.length>0&&i< storageOrgrequest.length ;i++){
       		StorageOrgUnitInfo info= (StorageOrgUnitInfo) storageOrgrequest[i];
       		if(info==null){
       			break;
       			}
       		else{
       			flag=true;
       		}
       		if(i<storageOrgrequest.length-1)
       			temp+=" '"+info.getId().toString()+"' ,";
       		else
       			temp+=" '"+info.getId().toString()+"' )";
       	}
       	if(flag)
       	condition.append(temp);
       	
       	String temp1="and T_SEL_SellPlan.FStorageOrgProvide in (";
       	for(int i=0 ;storageOrgprovide!=null&&i< storageOrgprovide.length ;i++){
       		StorageOrgUnitInfo info= (StorageOrgUnitInfo) storageOrgprovide[i];
       		if(i<storageOrgprovide.length-1)
       			temp1+=" '"+info.getId().toString()+"' ,";
       		else
       			temp1+=" '"+info.getId().toString()+"' )";
       	}
       	if(storageOrgprovide!=null&&storageOrgprovide.length>0)
       	condition.append(temp1);
//       	DateTimeUtils sf=new SimpleDateFormat("yyyy-MM-dd 00:00:00");
     
       	
       
       	if(params.getObject("startdate")!=null){
        Object startdate=	DateTimeUtils.format((Date) params.getObject("startdate"),"yyyy-MM-dd 00:00:00");
        condition.append(" and  T_SEL_SellPlan.FPlanstartdate >=to_date('"+startdate.toString()+"')");
        }
    	if(params.getObject("enddate")!=null){
        Object enddate=	DateTimeUtils.format((Date) params.getObject("enddate"),"yyyy-MM-dd 23:59:59");
        condition.append(" and  T_SEL_SellPlan.FPlanenddate<=to_date('"+enddate.toString()+"')");
        }
    	if(params.getObject("bizdatefrom")!=null){
    	Object bizdatefrom=	DateTimeUtils.format((Date) params.getObject("bizdatefrom"),"yyyy-MM-dd 00:00:00");
     
			condition.append(" and T_SEL_SellPlan.FBizDate>=to_date('"+bizdatefrom.toString()+"')");
		
        }
    	if(params.getObject("bizdateto")!=null){
    	Object bizdateto=	DateTimeUtils.format((Date) params.getObject("bizdateto"),"yyyy-MM-dd 23:59:59");
        condition.append(" and T_SEL_SellPlan.FBizDate<=to_date('"+bizdateto.toString()+"')");
        }
    
    
    	if(params.getObject("materialfrom")!=null){
            MaterialInfo  info=	(MaterialInfo) params.getObject("materialfrom");
            condition.append(" and t_bd_material.fnumber>= '"+info.getNumber()+"'");
            }
    	if(params.getObject("materialto")!=null){
    		   MaterialInfo  info=	(MaterialInfo) params.getObject("materialto");
               condition.append(" and t_bd_material.fnumber<= '"+info.getNumber()+"'");
            }
        if(params.getObject("billstatuse")!=null){
        	billstatus  info=	(billstatus) params.getObject("billstatuse");
        	    if(!billstatus.NULLVALUE_VALUE.equals(info.getValue()))
                condition.append(" and T_SEL_SellPlan.FBillstatus= '"+info.getValue()+"'");
             }
        List list=new ArrayList();
    	if(params.getBoolean("month")){
    		list.add("1");
    	
    	}
    	if(params.getBoolean("tendays1")){
    		list.add("2");
    	}
    	if(params.getBoolean("tendays2")){
    	  list.add("3");	
    	}
    	if(params.getBoolean("tendays3")){
    		list.add("4");
    	}
    	if(params.getBoolean("monthAdd")){
    		list.add("5");
    	}
        if(list.size()>0){
        	String p=" and T_SEL_SellPlan.FSellplantype in (";
        	for(int i=0 ; i< list.size() ;i++){
        		if(i<list.size()-1){
        			p+=list.get(i).toString()+",";
        		}
        		else
        			p+=list.get(i).toString()+")";
        			
        	}
        	 condition.append(p);
        }
        StringBuffer sql= new StringBuffer(
        	"select t_bd_material.fid as materialid,t_bd_material.fnumber as materialnumber,t_bd_material.fname_l2 as materialname,T_SEL_SellPlanEntry.FMaterialmodel as Materialmodel,\n" +
        	"T_BD_MeasureUnit1.fname_l2 as measurename,T_BD_MeasureUnit2.fname_l2 as basemeasurename ,\n" + 
        	"sum(FBasenum) AS FBasenum,SUM(FFivemonthnum)AS FFivemonthnum , SUM(FFourmonthnum) AS FFourmonthnum ,SUM(FThreemonthnum) AS FThreemonthnum,\n" + 
        	"SUM(FTwomonthnum) AS FTwomonthnum ,SUM(FOnemonthnum) AS FOnemonthnum ,SUM(FTotalnum) AS FTotalnum ,sum(FTotalpiecenum) as FTotalpiecenum,SUM(FOneweeknum) AS FOneweeknum,\n" + 
        	"SUM(FOneweekpiecenum) AS FOneweekpiecenum ,SUM(FSecondweeknum)  AS FSecondweeknum ,SUM(FSecondweekpiecenum) AS FSecondweekpiecenum,\n" + 
        	"SUM(FThirdweeknum) AS FThirdweeknum ,SUM(FThirdweekpiecenum) AS FThirdweekpiecenum ,SUM(FForthweeknum) AS FForthweeknum,\n" + 
        	"SUM(FForthweekpiecenum) AS FForthweekpiecenum ,SUM(FFirstAuditnum) AS FFirstAuditnum,SUM(FSecondAuditnum) AS FSecondAuditnum,\n" + 
        	"SUM(FThirdAuditnum) AS FThirdAuditnum ,SUM(FForthAuditnum) AS FForthAuditnum,SUM(FTotalauditnum) AS FTotalauditnum,\n" + 
        	"SUM(FTotalAuditbasenum) AS FTotalAuditbasenum ,SUM(FTotalauditpiecenum) AS FTotalauditpiecenum\n" + 
        	"from T_SEL_SellPlan\n" + 
        	"inner join T_SEL_SellPlanEntry\n" + 
        	"on T_SEL_SellPlanEntry.FParentID=T_SEL_SellPlan.fid\n" + 
        	"inner join t_bd_material on t_bd_material.fid=T_SEL_SellPlanEntry.FMaterialidID\n" + 
        	"left join T_BD_MeasureUnit  T_BD_MeasureUnit1 on T_BD_MeasureUnit1.fid=T_SEL_SellPlanEntry.FMeasureID\n" + 
        	"left join T_BD_MeasureUnit T_BD_MeasureUnit2 on T_BD_MeasureUnit2.fid=T_SEL_SellPlanEntry.FBasemeasueID\n" );
        sql.append(condition);
        sql.append(" group by t_bd_material.fid ,t_bd_material.fnumber ,t_bd_material.fname_l2 ,T_SEL_SellPlanEntry.FMaterialmodel,\n" + 
        	"T_BD_MeasureUnit1.fname_l2 ,T_BD_MeasureUnit2.fname_l2 ");
        sql.append(	" order by  t_bd_material.fnumber ,t_bd_material.fname_l2 ,T_SEL_SellPlanEntry.FMaterialmodel,\n" + 
    	"T_BD_MeasureUnit1.fname_l2 ,T_BD_MeasureUnit2.fname_l2  ");
        RptParams p = new RptParams();
        RptRowSet rptRowSet = executeQuery(sql.toString(), null, ctx);
		p.setObject("result", rptRowSet);
		return p;
    }
	@Override
	protected RptParams _getDetailSalePlan(Context ctx, RptParams params)
			throws BOSException {
		// TODO Auto-generated method stub
		StringBuffer condition=new StringBuffer(" where T_SEL_SellPlanEntry.FMaterialidID='"+params.getObject("materialid")+"' ");
       	Object[] storageOrgs =(Object[]) params.getObject("storageOrg");
       	String temp="and T_SEL_SellPlan.FStorageOrgRequest in (";
       	for(int i=0 ;storageOrgs!=null&&i< storageOrgs.length ;i++){
       		StorageOrgUnitInfo info= (StorageOrgUnitInfo) storageOrgs[i];
       		if(i<storageOrgs.length-1)
       			temp+=" '"+info.getId().toString()+"' ,";
       		else
       			temp+=" '"+info.getId().toString()+"' )";
       	}
       	if(storageOrgs!=null&&storageOrgs.length>0)
       		condition.append(temp);
//       	DateTimeUtils sf=new SimpleDateFormat("yyyy-MM-dd 00:00:00");
       
       	if(params.getObject("startdate")!=null){
        Object startdate=	DateTimeUtils.format((Date) params.getObject("startdate"),"yyyy-MM-dd 00:00:00");
        condition.append(" and  T_SEL_SellPlan.FPlanstartdate >=to_date('"+startdate.toString()+"')");
        }
    	if(params.getObject("enddate")!=null){
        Object enddate=	DateTimeUtils.format((Date) params.getObject("enddate"),"yyyy-MM-dd 23:59:59");
        condition.append(" and  T_SEL_SellPlan.FPlanenddate<=to_date('"+enddate.toString()+"')");
        }
    	if(params.getObject("bizdatefrom")!=null){
    	Object bizdatefrom=	DateTimeUtils.format((Date) params.getObject("bizdatefrom"),"yyyy-MM-dd 00:00:00");
     
			condition.append(" and T_SEL_SellPlan.FBizDate>=to_date('"+bizdatefrom.toString()+"')");
		
        }
    	if(params.getObject("bizdateto")!=null){
    	Object bizdateto=	DateTimeUtils.format((Date) params.getObject("bizdateto"),"yyyy-MM-dd 23:59:59");
        condition.append(" and T_SEL_SellPlan.FBizDate<=to_date('"+bizdateto.toString()+"')");
        }
    	if(params.getObject("billstatuse")!=null){
           	billstatus  info=	(billstatus) params.getObject("billstatuse");
           	    if(!billstatus.NULLVALUE_VALUE.equals(info.getValue()))
                   condition.append(" and T_SEL_SellPlan.FBillstatus= '"+info.getValue()+"'");
                }
    	StringBuffer sql= new StringBuffer("select T_SEL_SellPlan.fid as id from T_SEL_SellPlan inner join T_SEL_SellPlanEntry" +
        		" on T_SEL_SellPlanEntry.FParentID=T_SEL_SellPlan.fid ");
        sql.append(condition);
        RptParams p = new RptParams();
        RptRowSet rptRowSet = executeQuery(sql.toString(), null, ctx);
		p.setObject("result", rptRowSet);
		return p;
	}
}